package com.acme.bankapp.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class BankClient {

	private static final String ERR_CLASS_NOT_FOUND = "response from server is in unknown format";
	Socket requestSocket;
	ObjectOutputStream out;
	ObjectInputStream in;
	
	public BankClient(String host, int port) throws UnknownHostException, IOException {
		requestSocket = new Socket(host, port);
		out = new ObjectOutputStream(requestSocket.getOutputStream());
		in = new ObjectInputStream(requestSocket.getInputStream());
	}
	
	public String send(String message) throws IOException {
		out.writeObject(message);
		out.flush();
		
		String response;
		try {
			response = (String) in.readObject();
		} catch (ClassNotFoundException e) {
			response = ERR_CLASS_NOT_FOUND;
		}
		return response;
	}
	
	public void close() throws IOException {
		in.close();
		out.close();
		requestSocket.close();
	}

}
